<?php
namespace App\Http\Controllers;
use App\Exceptions\ValidateException;
use App\Repository\Contracts\RegistrationInterface;
use App\Support\Helper\CommonHelper;
use Illuminate\Http\Request;
use Auth;

/**
 * @Author:: HuangYin
 * @DateTime: 2017-01-17 20:07
 *
 * @Description: 登记
 */
class RegistrationController extends Controller
{
    /**
     * @var Request
     */
    protected $request;

    /**
     * @var RegistrationInterface
     */
    protected $registration;

    protected $uid;

    /**
     * RegistrationController constructor.
     * @param Request $request
     * @param RegistrationInterface $registration
     */
    public function __construct(Request $request, RegistrationInterface $registration)
    {
        $this->request = $request;

        $this->registration = $registration;

        $this->uid = CommonHelper::getToken(Auth::user())['uid'];
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function index()
    {
        $limit = $this->request->input('limit', 15);
        $page = $this->request->input('page', 1);
        $list = $this->registration->lists($limit, $page);

        return $this->success($list);
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail($id)
    {
        $detail = $this->registration->detail($id);

        return $this->success($detail);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function created()
    {
        try {
            $data = $this->request->only(['type', 'memo', 'uid']);
            $id = $this->registration->created($data);

            return $this->success($id);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }
}